﻿using System.Windows;
using WPFDBBrowser.Framework;
using WPFDBBrowser.Settings;
using WPFDBBrowser.Windows;
using System.ComponentModel;
using WPFDBBrowser.DBTreeView;
using System.Windows.Input;
using System.Diagnostics;
using System.Collections.Generic;
using WPFDBBrowser.BL.TreeView;
using System;
using WPFDBBrowser.Interfaces.DBTreeView;

namespace WPFDBBrowser.UserControls
{
    /// <summary>
    /// Interaction logic for DBTreeView.xaml
    /// </summary>
    public partial class DBTreeView : UserControlBase<DBTreeViewPresenter, IDBTreeView>, IDBTreeView
    {
        #region Commands
        public static RoutedCommand runSelectCmd = new RoutedCommand();
        public static RoutedCommand copySelectCmd = new RoutedCommand();
        public static RoutedCommand editConnectionCmd = new RoutedCommand();
        public static RoutedCommand loadTablesCmd = new RoutedCommand();
        public static RoutedCommand loadColumnsCmd = new RoutedCommand();
        #endregion

        CommandHelper _commandHelper;

        public DBTreeView()
            : this(null)
        {
        }

        public DBTreeView(string instanceName)
            : base(instanceName)
        {
            InitializeComponent();
            //this.treeViewDB.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(treeViewDB_SelectedItemChanged);
        }
        
        protected override void OnControlLoaded()
        {
            base.OnControlLoaded();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                // Sets tree view sorce fro presenter
                this.treeViewDB.ItemsSource = this.Presenter.GetConnections();
            }
        }
        protected override void RegisterCommands(Window window)
        {
            this._commandHelper = new CommandHelper(this.treeViewDB, this.ParentWindow);
            this._commandHelper.AddCommand(copySelectCmd,
                new Type[] { typeof(IColumnContainer) },
                this.ExecutedCopyCommand);
            this._commandHelper.AddCommand(runSelectCmd,
                new Type[] { typeof(IColumnContainer) },
                this.ExecutedRunCommand);
            this._commandHelper.AddCommand(editConnectionCmd,
                new Type[] { typeof(DBEntity) },
                this.ExecutedEditConnCommand);
            this._commandHelper.AddCommand(loadTablesCmd,
                new Type[] { typeof(DBEntity) },
                this.ExecutedLoadTablesCommand);
            this._commandHelper.AddCommand(loadColumnsCmd,
                new Type[] { typeof(IColumnContainer) },
                this.ExecutedLoadColumnsCommand);
            this.buttonScriptRun.Command = runSelectCmd;
            this.buttonScriptCopy.Command = copySelectCmd;
            this.buttonEditConnection.Command = editConnectionCmd;
            this.buttonLoadTables.Command = loadTablesCmd;
            this.buttonLoadColumns.Command = loadColumnsCmd;
        }
        //void treeViewDB_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        //{
        //}

        #region Commands methods
        private void ExecutedLoadTablesCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            DBEntity db = this.treeViewDB.SelectedItem as DBEntity;
            if (db != null)
            {
                this.Presenter.LoadTables(db);
                this.Presenter.LoadViews(db);
            }
        }
        private void ExecutedLoadColumnsCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            IColumnContainer db = this.treeViewDB.SelectedItem as IColumnContainer;
            if (db != null)
            {
                this.Presenter.LoadColumns(db);
            }
        }
        private void ExecutedEditConnCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            DBEntity dbEntity = (DBEntity)this.treeViewDB.SelectedItem;
            ConnectionEdit edit = new ConnectionEdit(new DBConnectionInfo(
                dbEntity.ConnectionString, dbEntity.Name, dbEntity.Type));
            bool? result = edit.ShowDialog();
            if (result.HasValue && result.Value)
            {
                dbEntity.Name = edit.DBInfo.Name;
                dbEntity.ConnectionString = edit.DBInfo.ConnectionString;
                dbEntity.Type = edit.DBInfo.Type;
            }
        }
        private void ExecutedCopyCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            IColumnContainer tableOrView = (IColumnContainer)this.treeViewDB.SelectedItem;
            if (!tableOrView.AreColumnsLoaded)
                this.Presenter.LoadColumns(tableOrView);
            this.Presenter.RunQuery((IColumnContainer)this.treeViewDB.SelectedItem, true);
        }
        private void ExecutedRunCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            IColumnContainer tableOrView = (IColumnContainer)this.treeViewDB.SelectedItem;
            if (!tableOrView.AreColumnsLoaded)
                this.Presenter.LoadColumns(tableOrView);
            this.Presenter.RunQuery(tableOrView, false);
        }
        #endregion

        private void buttonAddConnection_Click(object sender, RoutedEventArgs e)
        {
            ConnectionEdit edit = new ConnectionEdit(new DBConnectionInfo("WPFDBBrowser.OracleFactory.OracleDBFactory"));
            bool? result = edit.ShowDialog();
            if (result.HasValue && result.Value)
            {
                DBEntity newEntity = new DBEntity(edit.DBInfo.Type)
                {
                    Name = edit.DBInfo.Name,
                    ConnectionString = edit.DBInfo.ConnectionString
                };
                this.Presenter.AddConnection(newEntity);
                this.treeViewDB.Focus();
                newEntity.IsSelected = true;
                if (this.treeViewDB.ItemsSource == null)
                    this.treeViewDB.ItemsSource = this.Presenter.GetConnections();
            }
        }
    }
}
